package com.lanxiao.cloud.modules.dict.application.assembler;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.dto.UserBaseDto;
import com.lanxiao.cloud.common.fill.FillAppointment;
import com.lanxiao.cloud.common.fill.FillHandle;
import com.lanxiao.cloud.common.fill.FillType;
import com.lanxiao.cloud.modules.dict.application.dto.DictTypeCmdAdd;
import com.lanxiao.cloud.modules.dict.application.dto.DictTypeCmdUpdate;
import com.lanxiao.cloud.modules.dict.application.dto.DictTypePageDto;
import com.lanxiao.cloud.modules.dict.domain.model.DictType;
import com.lanxiao.cloud.user.api.fill.BaseDtoFillHandleFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-09-26
 */
@Component
public class DictTypeAssembler {

  private final BaseDtoFillHandleFactory<Long> fillHandleFactory;

  public DictTypeAssembler(
      BaseDtoFillHandleFactory<Long> fillHandleFactory) {
    this.fillHandleFactory = fillHandleFactory;
  }

  public DictType toDictType(DictTypeCmdAdd cmdAdd) {
    return DictTypeConverter.INSTANCE.toDictType(cmdAdd);
  }

  public DictType toDictType(Long id, DictTypeCmdUpdate cmdUpdate) {
    DictType dictType = DictTypeConverter.INSTANCE.toDictType(cmdUpdate);
    dictType.setId(id);
    return dictType;
  }

  public ResultModel<DictTypePageDto> toDictTypePageDto(IPage<DictType> page) {
    final List<DictTypePageDto> dictTypeDtos = DictTypeConverter.INSTANCE.toDictTypeDto(
        page.getRecords());
    fillHandleFactory.fillHandle(dictTypeDtos);
    return new ResultModel<>(dictTypeDtos, (int) page.getTotal());
  }

  @Mapper
  public interface DictTypeConverter {

    DictTypeConverter INSTANCE = Mappers.getMapper(DictTypeConverter.class);

    DictType toDictType(DictTypeCmdAdd cmdAdd);
    DictType toDictType(DictTypeCmdUpdate cmdUpdate);

    @Mappings({
        @Mapping(source = "createBy", target = "createUser.id"),
        @Mapping(source = "updateBy",target = "updateUser.id")
    })
    DictTypePageDto toDictTypeDto(DictType dictType);
    List<DictTypePageDto> toDictTypeDto(List<DictType> dictTypes);

  }

}
